package com.wxq.workpad.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashSet;

public class SpUtils {
    /**
     * 保存Preference的name
     */
    private static final String PREFERENCE_NAME = "saveInfo";
    private static SharedPreferences sp;
    private static volatile SpUtils mSharedPreferencesUtil;
    private static SharedPreferences.Editor editor;

    private SpUtils(Context context) {
        sp = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    /**
     * 单例模式，获取instance实例
     *
     * @return
     */
    public static SpUtils getInstance() {
        if (mSharedPreferencesUtil == null) {
            synchronized (SpUtils.class) {
                if (mSharedPreferencesUtil == null) {
                    mSharedPreferencesUtil = new SpUtils(ApplicationUtils.getApp());
                }
            }
        }
        return mSharedPreferencesUtil;
    }


    public void putString(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }

    public String getString(String key, String defValue) {
        return sp.getString(key, defValue);
    }

    public void putInt(String key, int value) {
        editor.putInt(key, value);
        editor.commit();
    }

    public int getInt(String key,int defValue) {
        return sp.getInt(key, defValue);
    }

    public void putLong(String key, long value) {
        editor.putLong(key, value);
        editor.commit();
    }

    public long getLong(String key,long defValue) {
        return sp.getLong(key, defValue);
    }


    public void putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.commit();
    }

    public boolean getBoolean(String key,boolean defValue) {
        return sp.getBoolean(key, defValue);
    }


    public void putFloat(String key, float value) {
        editor.putFloat(key, value);
        editor.commit();
    }

    public float getFloat(String key, float defValue) {
        return sp.getFloat(key, defValue);
    }


    public void putStringSet(String key, HashSet<String> strings) {
        editor.putStringSet(key, strings);
        editor.commit();
    }

    public HashSet<String> getStringSet(String key, HashSet<String> defValues) {
        return (HashSet<String>) sp.getStringSet(key, defValues);
    }


    //鉴于聊天（环信/千信）消息到达本机可能比请求今日头条插入消息来得慢，会导致消息重复的情况
    //保存一份已经从接口请求到消息的service_message_id列表，今后收到相同消息，不存入数据库
    public static final String DAILY_INSERT_MESSAGE_IDS = "daily_insert_message_ids";
    /**
     * 将本地保存的 从接口那边取到的 今日头条历史消息的ID列表取出来，用于收到消息后查重不存入数据库
     *
     * @param serviceMessageId 聊天消息ext中 service_message_id
     * @return 聊天消息是否和从接口中获得的今日头条重复
     */

//    public  boolean hasDailyMessage(String serviceMessageId) {
//        if (StringUtils.isEmpty(serviceMessageId)) {
//            return false;
//        }
//        String messageIds = SpUtils.getInstance().getString(DAILY_INSERT_MESSAGE_IDS, "");
//        return messageIds.contains(serviceMessageId);
//    }
}
